package com.wfm.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

public class POne31分割回文串{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

    List<List<String>> res = new ArrayList<>();
    List<String> path = new ArrayList<>();

    public List<List<String>> partition(String s) {
        dfs(s, 0);
        return res;
    }

    void dfs(String s, int start) {
        if (start >= s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < s.length(); i++) {
            if (isPalindrome(s, start, i)) {
                path.add(s.substring(start, i + 1));
                dfs(s, i + 1);
                path.remove(path.size() - 1);
            } else {
                continue;
            }
        }
    }

    //判断字符串从[left，right]的子串是否是回文的
    public boolean isPalindrome(String str, int left, int right) {
        while (left < right) {
            if (str.charAt(left++) != str.charAt(right--))
                return false;
        }
        return true;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new POne31分割回文串().new Solution();
    }
}